﻿using Vowei.Web.Controllers;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using Vowei.Core.Models;
using System.Web.Mvc;
using Microsoft.VisualStudio.TestTools.UnitTesting.Web;
using Vowei.Data;
using Vowei.Data.Models;
using System.Linq;

namespace vowei.test
{
    [TestClass()]
    public class TicketControllerBaseTest : TestBase
    {
        public void CreateWithNullProjectTestHelper<T>()
            where T : Ticket, new()
        {
            var target = new TicketControllerBase<T>(VoweiContext, Setting.DefaultFormUser);
            ActionResult actual = target.Create();
            var result = actual as ViewResult;

            Assert.IsNotNull(result);
            Assert.IsNotNull(result.ViewData.Model);
            Assert.IsTrue(result.ViewData.Model is T);
        }

        [TestMethod()]
        public void CreateWithNullProjectTest()
        {
            CreateWithNullProjectTestHelper<Ticket>();
            CreateWithNullProjectTestHelper<Requirement>();
            CreateWithNullProjectTestHelper<Task>();
            CreateWithNullProjectTestHelper<Bug>();
            //CreateWithNullProjectTestHelper<MeetingRequest>();
        }
                
        [TestMethod()]
        public void CreateWithProjectTest()
        {
            var target = new RequirementController(VoweiContext, Setting.DefaultFormUser);
            Project project = null;
            using (var context = new VoweiContextImpl())
            {
                project = GetDefaultProject(context);
            }

            var actual = target.CreateIn(project.ID);
            var result = actual as ViewResult;

            Assert.IsNotNull(result);
            Assert.IsNotNull(result.ViewData.Model);
            Assert.IsTrue(result.ViewData.Model is Requirement);

            var ticket = result.ViewData.Model as Requirement;
            Assert.IsFalse(ticket.ID.CompareTo(Guid.Empty) == 0);
            Assert.IsNull(ticket.BelongsToMilestone);
            Assert.IsNotNull(ticket.Comments);
            Assert.AreEqual(0, ticket.Comments.Count());
        }

        public void DetailsWithProjectTestHelper<T>(Guid id)
            where T : Ticket, new()
        {
            var target = new TicketControllerBase<T>(VoweiContext, Setting.DefaultFormUser);
            ActionResult actual = target.Details(id);
            var result = actual as ViewResult;

            Assert.IsNotNull(result);
            Assert.IsNotNull(result.ViewData.Model);
            Assert.IsTrue(result.ViewData.Model is T);


            var ticket = result.ViewData.Model as T;
            Assert.IsFalse(string.IsNullOrEmpty(ticket.Title));
            Assert.IsNotNull(ticket.Comments);
            Assert.AreEqual(0, ticket.Comments.Count());

            if (ticket is IContainer<T>)
            {
                var container = ticket as IContainer<T>;
                Assert.IsNotNull(container.Children);
                Assert.AreEqual(0, container.Children.Count());
            }
        }

        [TestMethod()]
        public void DetailsWithProjectTest()
        {
            Guid rid, tid, bid = Guid.Empty;
            using (var context = new VoweiContextImpl())
            {
                var project = new Project()
                {
                    Title = "DetailsWithProjectTest_Project",
                    Owner = Setting.DefaultUserName,
                    StartDate = DateTime.Now,
                    DueDate = DateTime.Now + TimeSpan.FromDays(30)
                };
                var milestone = new Milestone()
                {
                    Title = "DetailsWithProjectTest_Milestone",
                    StartDate = DateTime.Now,
                    DueDate = DateTime.Now + TimeSpan.FromDays(30)
                };
                project.Children.Add(milestone);
                context.Projects.Add(project);

                var requirement = new Requirement()
                {
                    Title = "DetailsWithProjectTest_Requirement",
                    AssignedTo = Setting.DefaultUserName,
                    BusinessValue = 1234,
                    BelongsToMilestone = milestone
                };
                context.Requirements.Add(requirement);

                var task = new Task()
                {
                    Title = "DetailsWithProjectTest_Task",
                    AssignedTo = Setting.DefaultUserName,
                    DueDate = DateTime.Now
                };
                context.Tasks.Add(task);

                var bug = new Bug()
                {
                    Title = "DetailsWithProjectTest_Bug",
                    AssignedTo = Setting.DefaultUserName,
                };
                context.Bugs.Add(bug);
                context.SaveChanges();
                rid = requirement.ID;
                bid = bug.ID;
                tid = task.ID;
            }

            DetailsWithProjectTestHelper<Requirement>(rid);
            DetailsWithProjectTestHelper<Task>(tid);
            DetailsWithProjectTestHelper<Bug>(bid);
        }
    }
}
